package com.ruoyi.web.controller.lims;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lims.domain.LimsOperateComputerInfo;
import com.ruoyi.lims.domain.LimsOperateComputerInfoExcel;
import com.ruoyi.lims.service.ILimsOperateComputerInfoExcelService;
import com.ruoyi.lims.service.ILimsOperateComputerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

/**
 * 运营上机信息Controller
 * 
 * @author wry
 * @date 2023-07-25
 */
@RestController
@RequestMapping("/lims/computerinfo")
public class LimsOperateComputerInfoController extends BaseController
{
    @Autowired
    private ILimsOperateComputerInfoService limsOperateComputerInfoService;
    @Autowired
    private ILimsOperateComputerInfoExcelService limsOperateComputerInfoExcelService;
    /**
     * 查询运营上机信息列表
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(LimsOperateComputerInfo limsOperateComputerInfo)
    {
        startPage();
        List<LimsOperateComputerInfo> list = limsOperateComputerInfoService.selectLimsOperateComputerInfoList(limsOperateComputerInfo);
        return getDataTable(list);
    }

    /**
     * 查询上机池-不包lane列表
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:listNolane')")
    @GetMapping("/listNolane")
    public TableDataInfo listNolane(LimsOperateComputerInfo limsOperateComputerInfo)
    {
        startPage();
        List<LimsOperateComputerInfo> list = limsOperateComputerInfoService.selectLimsOperateComputerInfoNoLaneList(limsOperateComputerInfo);
        return getDataTable(list);
    }

    /**
     * 查询上机池-laneNum列表
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:listLaneNum')")
    @GetMapping("/listLaneNum")
    public TableDataInfo listLaneNum(LimsOperateComputerInfo limsOperateComputerInfo)
    {
        startPage();
        List<LimsOperateComputerInfo> list = limsOperateComputerInfoService.selectLimsOperateComputerInfoLaneNumList(limsOperateComputerInfo);
        return getDataTable(list);
    }


    /**
     * 查询上机池-包lane列表
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:listHaveLane')")
    @GetMapping("/listHaveLane")
    public TableDataInfo listHaveLane(LimsOperateComputerInfo limsOperateComputerInfo)
    {
        startPage();
        List<LimsOperateComputerInfo> list = limsOperateComputerInfoService.selectLimsOperateComputerInfolistHaveLaneList(limsOperateComputerInfo);
        return getDataTable(list);
    }

    /**
     * 查询任务下发列表
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:listByBatchid')")
    @GetMapping("/listByBatchid")
    public TableDataInfo listByBatchid(LimsOperateComputerInfo limsOperateComputerInfo)
    {
        startPage();
        List<LimsOperateComputerInfo> list = limsOperateComputerInfoService.selectLimsOperateComputerInfoList(limsOperateComputerInfo);
        return getDataTable(list);
    }

    /**
     * 导出运营上机信息列表
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:export')")
    @Log(title = "运营上机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportEdit")
    public void exportEdit(HttpServletResponse response, LimsOperateComputerInfoExcel limsOperateComputerInfoExcel)
    {
        LambdaQueryWrapper<LimsOperateComputerInfoExcel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(LimsOperateComputerInfoExcel::getEnable,"-1");
        queryWrapper.eq(limsOperateComputerInfoExcel.getBatchId()!=null,LimsOperateComputerInfoExcel::getBatchId, limsOperateComputerInfoExcel.getBatchId());
        queryWrapper.in(limsOperateComputerInfoExcel.getIdList()!=null,LimsOperateComputerInfoExcel::getComputerId, limsOperateComputerInfoExcel.getIdList());        ;
        ExcelUtil<LimsOperateComputerInfoExcel> util = new ExcelUtil<LimsOperateComputerInfoExcel>(LimsOperateComputerInfoExcel.class);
        util.setLock(true);
        util.exportExcel(response, limsOperateComputerInfoExcelService.list(queryWrapper), "运营上机信息数据");
    }

    /**
     * 导出信息
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:export')")
    @Log(title = "运营上机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LimsOperateComputerInfo limsOperateComputerInfo)
    {
        List<LimsOperateComputerInfo> list = limsOperateComputerInfoService.selectLimsOperateComputerInfoList(limsOperateComputerInfo);
        ExcelUtil<LimsOperateComputerInfo> util = new ExcelUtil<LimsOperateComputerInfo>(LimsOperateComputerInfo.class);
        util.exportExcel(response, list, "运营上机信息数据");
    }

    /**
     * 获取运营上机信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:query')")
    @GetMapping(value = "/{computerId}")
    public AjaxResult getInfo(@PathVariable("computerId") Long computerId)
    {
        return success(limsOperateComputerInfoService.getById(computerId));
    }

    /**
     * 新增运营上机信息
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:add')")
    @Log(title = "运营上机信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LimsOperateComputerInfo limsOperateComputerInfo)
    {
        return toAjax(limsOperateComputerInfoService.save(limsOperateComputerInfo));
    }

    /**
     * 修改运营上机信息
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:edit')")
    @Log(title = "运营上机信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LimsOperateComputerInfo limsOperateComputerInfo)
    {
        String message =limsOperateComputerInfoService.updateLimsOperateComputerInfo(limsOperateComputerInfo);
        return success(message);
    }
    /**
     * 修改任务下发
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:updateLisComputer')")
    @Log(title = "任务下发批量修改", businessType = BusinessType.UPDATE)
    @PutMapping("/{updateListInfo}")
    public AjaxResult updateListInfo(@RequestBody LimsOperateComputerInfo limsOperateComputerInfo)
    {
        limsOperateComputerInfo.setUpdateBy(getUsername());
        String message =limsOperateComputerInfoService.updateLimsOperateComputerInfoList(limsOperateComputerInfo);
        return success(message);
    }
    /**
     * 删除运营上机信息
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:remove')")
    @Log(title = "运营上机信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{computerIds}")
    public AjaxResult remove(@PathVariable Collection<Long> computerIds)
    {
        return toAjax(limsOperateComputerInfoService.deleteLimsOperateComputerInfoBycomputerIds(computerIds));
    }

    @Log(title = "运营上机信息-导入运营信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('computerinfo:info:import')")
    @PostMapping("/importLimsOperateComputerInfo")
    public AjaxResult importLimsOperateComputerInfo(MultipartFile file, String batchId) throws Exception
    {
        ExcelUtil<LimsOperateComputerInfo> util = new ExcelUtil<LimsOperateComputerInfo>(LimsOperateComputerInfo.class);
        List<LimsOperateComputerInfo> computerList = util.importExcel(file.getInputStream());
        String message = limsOperateComputerInfoService.importLimsOperateComputerInfo(computerList,batchId,getUsername());
        return success(message);
    }

    @Log(title = "运营上机信息-上传附件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('computerinfo:info:importAttachment')")
    @PostMapping("/importAttachment")
    public AjaxResult importAttachment(MultipartFile file, HttpServletRequest request) throws Exception
    {
        return success( limsOperateComputerInfoService.importAttachment(file, request.getParameter("ids")));
    }
    /**
     * 周期计算
     */
    @Log(title = "运营上机信息-周期计算", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('computerinfo:info:cycleCount')")
    @PostMapping("/cycleCount")
    public void cycleCount(LimsOperateComputerInfo info)
    {
        limsOperateComputerInfoService.doCycleCount(info.getBatchId(),null);
    }

    /**
     * 生成Lane编号
     */
    @Log(title = "运营上机信息-生成Lane编号", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('computerinfo:info:createLane')")
    @GetMapping("/createLane/{computerIds}")
    public void createLane(@PathVariable Collection<Long> computerIds)
    {
        limsOperateComputerInfoService.createLane(computerIds);
    }
    /**
     * 计算汉明距离
     */
    @Log(title = "运营上机信息-计算汉明距离", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('computerinfo:info:computerHm')")
    @GetMapping("/computerHm")
    public AjaxResult computerHm()
    {
        return success(limsOperateComputerInfoService.computerHm());
    }
    /**
     * 计算最小碱基平衡
     */
    @Log(title = "运营上机信息-计算最小碱基平衡", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('computerinfo:info:baseMinEquilibrium')")
    @GetMapping("/baseMinEquilibrium")
    public AjaxResult baseMinEquilibrium()
    {
        return success( limsOperateComputerInfoService.baseMinEquilibrium());
    }
    /**
     * 计算碱基平衡
     */
    @Log(title = "运营上机信息-计算碱基平衡", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('computerinfo:info:baseEquilibrium')")
    @PostMapping("/baseEquilibrium")
    public void baseEquilibrium(HttpServletResponse response)
    {
        try {
            limsOperateComputerInfoService.baseEquilibrium(response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 手动添加上机方案
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:toAddNolaneProgram')")
    @Log(title = "运营上机信息", businessType = BusinessType.UPDATE)
    @GetMapping("/toAddNolaneProgram/{computerIds}")
    public void toAddNolaneProgram(@PathVariable Collection<Long> computerIds)
    {
       limsOperateComputerInfoService.toAddNolaneProgram(computerIds);
    }

    /**
     * 查询上机方案-不包lane列表
     */
    @PreAuthorize("@ss.hasPermi('computerinfo:info:listPgNolane')")
    @GetMapping("/listPgNoLane")
    public TableDataInfo listPgNoLane(LimsOperateComputerInfo limsOperateComputerInfo)
    {
        startPage();
        List<LimsOperateComputerInfo> list = limsOperateComputerInfoService.transformData(limsOperateComputerInfo);
        return getDataTable(list);
    }
}
